package com.insurance.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.insurance.bean.Reply;
import com.insurance.service.ReplyService;


@Transactional
public class ReplyServiceBean implements ReplyService {
	@Resource private SessionFactory sessionFactory;

	public void save(Reply blog){
		sessionFactory.getCurrentSession().persist(blog);
	}
	
	public void update(Reply blog){
		sessionFactory.getCurrentSession().merge(blog);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Reply getReply(Integer blogid){
		return (Reply)sessionFactory.getCurrentSession().get(Reply.class, blogid);
	}

	public void delete(Integer blogid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Reply.class, blogid));
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Reply> getReplys(){		
		return sessionFactory.getCurrentSession().createQuery("from Reply").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Reply> getReplysByUserName(String userName){		
		return sessionFactory.getCurrentSession().createQuery("from Reply where userName="+"userName").list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Reply> getReplysByQuestionId(Integer questionId){		
		return sessionFactory.getCurrentSession().
		createQuery("from Reply r where r.questionId="+questionId).list();
	}
	
	public int getTotalCountByUserName(String userName) {
		
		return ((Long)sessionFactory.getCurrentSession().createQuery
				("select count(*) from Reply r where r.userName="+"userName").uniqueResult()).intValue();
		
	}
}
